package com.mkl.business.controller;

import com.mkl.business.domain.Sales;
import com.mkl.business.service.SalesService;
import com.mkl.business.vo.SalesVo;
import com.mkl.system.common.ActiveUser;
import com.mkl.system.common.ResultObj;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author king
 * @version 1.0
 * @date 2020/4/8 16:18
 */
@RestController
@RequestMapping("api/sales")
public class SalesController {

    @Autowired
    private SalesService salesService;

    /**
     * 查询全部
     *
     * @param salesVo 条件
     * @return 返回结果
     */
    @RequestMapping("loadAllSales")
    public Object loadAllSales(SalesVo salesVo) {
        return salesService.queryAllSales(salesVo);
    }

    /**
     * 添加商品销售信息
     *
     * @param sales 商品销售
     * @return 返回结果
     */
    @RequestMapping("addSales")
    public ResultObj addSales(Sales sales) {
        try {
            ActiveUser activeUser = (ActiveUser) SecurityUtils.getSubject().getPrincipal();
            sales.setOperateperson(activeUser.getUser().getName());
            sales.setSalestime(new Date());
            salesService.saveSales(sales);
            return ResultObj.ADD_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.ADD_ERROR;
        }

    }

    /**
     * 更新商品销售
     *
     * @param sales 商品销售信息
     * @return 返回结果
     */
    @RequestMapping("updateSales")
    public ResultObj updateSales(Sales sales) {
        try {
            salesService.updateSales(sales);
            return ResultObj.UPDATE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.UPDATE_ERROR;
        }

    }

    /**
     * 删除商品销售信息
     *
     * @param id id
     * @return 返回结果
     */
    @RequestMapping("deleteSales")
    public ResultObj deleteSales(Integer id) {
        try {
            salesService.removeById(id);
            return ResultObj.DELETE_SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            return ResultObj.DELETE_ERROR;
        }
    }


    /**
     * 查询销售数量
     *
     * @return 返回所有商品
     */
    @GetMapping("getAllAvailableSalesSum")
    public Integer getAllAvailableSalesSum() {
        return salesService.getAllAvailableSalesSum();
    }
}
